import $S from '@/common/lib/storage.js'
// import UserApi from '@/common/api/home/User.js'

export default {
	state: {
		accessToken: null,
		isLogin: false,
		userInfo: null
	},
	mutations: {
		
	},
	actions: {
		login({ state,dispatch },data){
			// 登录操作
			let token = data.access_token
			console.log(token)
			$S.setStorage('csrc_token',token)
			state.accessToken = data.accessToken
			dispatch('getUserInfo',{})
		},
		getUserInfo({ state,dispatch }){
			// UserApi.getUserInfo().then(result=>{
			// 	let user = result.value.userInfo
			// 	state.userInfo = user;
			// 	state.isLogin = true;
				
			// 	uni.$emit('onLogin',{})
			// })
		},
		logout({ state }){
			// 登出
			$S.removeStorage('csrc_token')
			state.isLogin = false
			state.userInfo = null
			uni.$emit('onLogout',{})
		},
		initLogin({ state,dispatch }){
			// 初始化登录状态
			
			let token = $S.getStorage('csrc_token');
			if(token){
				dispatch('getUserInfo',{})
			}else{
				console.log('用户未登录---')
			}
		}
	}
}